home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1618 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why use private class members instead of protected?
  5. Date: 11 Jan 1996 19:56:10 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Message-ID: <4d3q0q$uj@clarknet.clark.net>
  8. References: <30F4AB49.6ABB@sierra.net>
  9. NNTP-Posting-Host: explorer.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  14.  
  15. TGColwell (snowbull@sierra.net) wrote:
  16. : I'm relatively new to c++.  I have one quick question:  If child 
  17. : classes can only access protected members of the parent class, 
  18. : why make any members of any class private?  Wouldn't it be 
  19. : better to make members of the parent class protected so that the 
  20. : class is alway "inheritance ready"?
  21.  
  22. No.
  23.  
  24. Suppose I market a Date class. I allow the date to be set through a 
  25. public or protected function setdate(int m, int d, int y), or perhaps 
  26. through a similar overloaded assignment operator and a copy constructor. 
  27. I allow you to retrieve the date through a variety of functions in a 
  28. number of formats, both string and numeric. 
  29.  
  30. I store the date in three separate member variables: mon_, day_ and year_. 
  31.  
  32.  
  33. Suppose you wrote code that referred to mon_, day_ and year_. What would 
  34. you do when the next version of my library, which you load over the old 
  35. one, stores the date as a Julian long integer, and no longer HAS this 
  36. threesome?
  37.  
  38. My Date class has a variety of functions that return results like "first 
  39. day of the month following this date" and "number of days from this date 
  40. to another date". I have tuned these functions so that they always work 
  41. correctly--IF the Date object is in a legal state. To assure that this is 
  42. true, I have implemented data checking so that if you try to execute, for 
  43. example, setdate(42, 49, -3), an exception will be produced. But if you 
  44. could just go setting mon_ on your own, there would be nothing to stop 
  45. you from setting it to 42.
  46.  
  47. In other words, it's none of your business whether my class contains mon_ 
  48. or julian_date or any other implementation detail. These are private. 
  49. Your only concern is with the protected and public data (if any) and 
  50. functions that give my class the facilities that it is supposed to 
  51. provide you with.
  52.